home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Bitmap / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.8 KB  |  465 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "Bitmap.hpp"
  13.  
  14. #include "Binding.k"
  15.  
  16. #ifndef CONTENT_H
  17. #include "Content.h"
  18. #endif
  19.  
  20. #ifndef PART_H
  21. #include "Part.h"
  22. #endif
  23.  
  24. #ifndef SELECT_H
  25. #include "Select.h"
  26. #endif
  27.  
  28. // ----- Part Layer -----
  29.  
  30. #ifndef FWUTIL_H
  31. #include "FWUtil.h"
  32. #endif
  33.  
  34. #ifndef FWKIND_H
  35. #include "FWKind.h"
  36. #endif
  37.  
  38. // ----- OS Layer -----
  39.  
  40. #ifndef FWODMISC_H
  41. #include "FWODMisc.h"
  42. #endif
  43.  
  44. #ifndef FWPICTUR_H
  45. #include "FWPictur.h"
  46. #endif
  47.  
  48. #ifndef FWBARRAY_H
  49. #include "FWBArray.h"
  50. #endif
  51.  
  52. #ifndef FWFILEAC_H
  53. #include "FWFileAc.h"
  54. #endif
  55.  
  56. #ifndef SLMixOS_H
  57. #include "SLMixOS.h"
  58. #endif
  59.  
  60. #ifndef FWFILESY_H
  61. #include "FWFileSy.h"
  62. #endif
  63.  
  64. #ifndef FWFILREP_H
  65. #include "FWFilRep.h"
  66. #endif
  67.  
  68. // ----- Foundation Layer -----
  69.  
  70. #ifndef FWSTREAM_H
  71. #include "FWStream.h"
  72. #endif
  73.  
  74. #ifndef FWMEMORY_H
  75. #include "FWMemory.h"
  76. #endif
  77.  
  78. // ----- OpenDoc Includes -----
  79.  
  80. #ifndef SOM_Module_OpenDoc_StdProps_defined
  81. #include <StdProps.xh>
  82. #endif
  83.  
  84. #ifndef SOM_ODTranslation_xh
  85. #include <Translt.xh>
  86. #endif
  87.  
  88. #ifndef SOM_ODSession_xh
  89. #include <ODSessn.xh>
  90. #endif
  91.  
  92. #include "odfjpeg.h"
  93.  
  94. //========================================================================================
  95. //    Runtime Information
  96. //========================================================================================
  97.  
  98. #ifdef FW_BUILD_MAC
  99. #pragma segment odfbitmap
  100. #endif
  101.  
  102. //========================================================================================
  103. //    class CBitmapContent
  104. //========================================================================================
  105.  
  106. FW_DEFINE_AUTO(CBitmapContent)
  107.  
  108. //----------------------------------------------------------------------------------------
  109. //    CBitmapContent::CBitmapContent
  110. //----------------------------------------------------------------------------------------
  111. //    CBitmapContent constructor
  112.  
  113. CBitmapContent::CBitmapContent(Environment* ev, CBitmapPart* part) :
  114.     FW_CContent(ev, part),
  115.     fBitmapPart(part)
  116. {
  117.     FW_END_CONSTRUCTOR
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. //    CBitmapContent::~CBitmapContent
  122. //----------------------------------------------------------------------------------------
  123. //    CBitmapContent destructor
  124.  
  125. CBitmapContent::~CBitmapContent()
  126. {
  127.     FW_START_DESTRUCTOR
  128. }
  129.  
  130. //----------------------------------------------------------------------------------------
  131. //    CBitmapContent::GetBitmap
  132. //----------------------------------------------------------------------------------------
  133.  
  134. FW_CBitmap CBitmapContent::GetBitmap(Environment* ev) const
  135. {
  136.     if ((const void*)fBitmap == NULL)
  137.     {
  138.         // Fool the compiler so we can do caching
  139.         CBitmapContent* self = (CBitmapContent*)this;
  140.         self->UseDefaultBitmap(ev);
  141.     }
  142.     
  143.     return fBitmap;
  144. }
  145.  
  146. //---------------------------------------------------------------------------------------
  147. // CBitmapContent::SetBitmap
  148. //---------------------------------------------------------------------------------------
  149.  
  150. void CBitmapContent::SetBitmap (Environment* ev, FW_CBitmap bitmap)
  151. {
  152. FW_UNUSED(ev);
  153.     fBitmap = bitmap;
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. //    CBitmapContent::UseDefaultBitmap
  158. //----------------------------------------------------------------------------------------
  159. void CBitmapContent::UseDefaultBitmap(Environment* ev)
  160. {
  161.     FW_PSharedLibraryResourceFile resFile(ev);
  162.     
  163. #ifdef FW_BUILD_MAC
  164.     FW_CPicture picture(resFile, 2128);
  165.     fBitmap = FW_CBitmap(picture);
  166. #endif
  167. #ifdef FW_BUILD_WIN
  168.     fBitmap = FW_CBitmap(resFile, 2128);
  169. #endif
  170. }
  171.  
  172. //----------------------------------------------------------------------------------------
  173. //    CBitmapContent::DoExternalize
  174. //----------------------------------------------------------------------------------------
  175.  
  176. void CBitmapContent::DoExternalize(Environment* ev,
  177.                             ODStorageUnit* storageUnit,
  178.                             FW_CKind* kind,
  179.                             FW_CBitmap bitmap)
  180. {
  181.     storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kind->GetType(ev), 0, kODPosUndefined);
  182.     FW_SUClearValue(ev, storageUnit, kODPropContents, kind->GetType(ev));
  183.     
  184.     // To test if fBitmap is NULL, cast it to a const void*
  185.     if ((const void*)fBitmap != NULL)
  186.     {
  187.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  188.         FW_CWritableStream stream(suSink);
  189.         
  190.         if (kind->IsPartKind(ev))
  191.             stream << bitmap;
  192.         else if (kind->IsEqual(ev, kPICTOSType, kODPlatformDataType))
  193.             ExternalizePICTData(ev, stream, bitmap);
  194.         else
  195.             FW_DEBUG_MESSAGE("CBitmapContent::DoExternalize - Unknown type");
  196.     }
  197. }
  198.  
  199. //----------------------------------------------------------------------------------------
  200. //    CBitmapContent::ExternalizeKind
  201. //----------------------------------------------------------------------------------------
  202. //    ODF method
  203.  
  204. void CBitmapContent::ExternalizeKind(Environment* ev,
  205.                                   ODStorageUnit* storageUnit,
  206.                                   FW_CKind* kind,
  207.                                   FW_StorageKinds storageKind,
  208.                                   FW_CPromise* promise,
  209.                                   FW_CCloneInfo* cloneInfo)
  210. {
  211. FW_UNUSED(storageKind);
  212. FW_UNUSED(cloneInfo);
  213. FW_UNUSED(promise);
  214.  
  215.     DoExternalize(ev, storageUnit, kind, fBitmap);
  216. }
  217.  
  218. #ifdef FW_BUILD_MAC
  219. //---------------------------------------------------------------------------------------
  220. //    CBitmapContent::ExternalizePICTData
  221. //---------------------------------------------------------------------------------------
  222.  
  223. void CBitmapContent::ExternalizePICTData(Environment* ev, FW_CWritableStream& stream, const FW_CBitmap& bitmap)
  224. {
  225. FW_UNUSED(ev);
  226.     FW_CPicture picture = bitmap.MacGetAsPicture();
  227.     
  228.     FW_PlatformPict platformPict = picture.GetPlatformPict();
  229.  
  230.     unsigned long pictSize = FW_CMemoryManager::GetSystemHandleSize((FW_PlatformHandle)platformPict);
  231.     
  232.     FW_CAcquireLockedSystemHandle lockedHandle((FW_PlatformHandle)platformPict);
  233.     stream.Write(lockedHandle.GetPointer(), pictSize);    
  234. }
  235. #endif
  236.  
  237. //----------------------------------------------------------------------------------------
  238. //    CBitmapContent::InternalizeKind
  239. //----------------------------------------------------------------------------------------
  240. //    ODF method
  241.  
  242. FW_Boolean CBitmapContent::InternalizeKind(Environment* ev,
  243.                                     ODStorageUnit* storageUnit,
  244.                                     FW_CKind* kind, 
  245.                                      FW_StorageKinds storageKind,
  246.                                     FW_CCloneInfo* cloneInfo)
  247. {
  248. FW_UNUSED(storageKind);
  249. FW_UNUSED(cloneInfo);
  250.     if (kind->IsPartKind(ev)) {
  251.         InternalizeBitmapShape(ev, storageUnit, kind);
  252.     }
  253.     else if (kind->IsEqual(ev, kPICTOSType, kODPlatformDataType)) {
  254.         InternalizePICTData(ev, storageUnit, kind);
  255.     }
  256.     else if (kind->IsEqual(ev, kPICTOSType, kODPlatformFileType)) {
  257.         InternalizePICTFile(ev, storageUnit);
  258.     }
  259.     else if (kind->IsEqual(ev, kJPEGOSType, kODPlatformDataType) ||
  260.             kind->IsEqual(ev, kJFIFOSType, kODPlatformDataType)) {
  261.         FW_PStorageUnitSink sink (ev, storageUnit, kODPropContents, kind->GetType(ev));
  262.         FW_CBitmap bitmap = ODF_JPEG_Read (ev, sink);
  263.         SetBitmap (ev, bitmap);
  264.     }
  265.     else if (kind->IsEqual(ev, kJPEGOSType, kODPlatformFileType) ||
  266.             kind->IsEqual(ev, kJFIFOSType, kODPlatformFileType)) {
  267.         FW_PFileValueSink sink(ev, storageUnit);
  268.         FW_CBitmap bitmap = ODF_JPEG_Read (ev, sink);
  269.         SetBitmap (ev, bitmap);
  270.     }
  271.     else 
  272.     {
  273.         FW_DEBUG_MESSAGE("CBitmapContent::InternalizeKind - Unknown type");
  274.         return false;
  275.     }
  276.     
  277.     return true;
  278. }
  279.  
  280. //---------------------------------------------------------------------------------------
  281. //    CBitmapContent::InternalizeBitmapShape
  282. //---------------------------------------------------------------------------------------
  283.  
  284. void CBitmapContent::InternalizeBitmapShape(Environment* ev, ODStorageUnit* storageUnit, FW_CKind* kind)
  285. {
  286.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  287.     if (suSink->GetReadableBytes(ev) != 0)
  288.     {
  289.         FW_PBufferedSink sink(ev, suSink);
  290.         FW_CReadableStream stream(sink);
  291.         stream >> fBitmap;
  292.     }
  293. }
  294.  
  295. #ifdef FW_BUILD_MAC
  296. //---------------------------------------------------------------------------------------
  297. //    CBitmapContent::InternalizePICTData
  298. //---------------------------------------------------------------------------------------
  299.  
  300. void CBitmapContent::InternalizePICTData(Environment* ev, ODStorageUnit* storageUnit, FW_CKind* kind)
  301. {
  302.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, kind->GetType(ev));
  303.     FW_PBufferedSink sink(ev, suSink);
  304.     FW_CReadableStream stream(sink);
  305.  
  306.     unsigned long pictSize = storageUnit->GetSize(ev);    //sink.GetLength();
  307.  
  308.     FW_CAcquireTemporarySystemHandle handle(pictSize);
  309.  
  310.     stream.Read(handle.GetPointer(), pictSize);
  311.     
  312.     MacSetBitmap(ev, (FW_PlatformPict)handle.GetPlatformHandle());
  313. }
  314. #endif
  315.  
  316. #ifdef FW_BUILD_MAC
  317. //---------------------------------------------------------------------------------------
  318. //    CBitmapContent::InternalizePICTFile
  319. //---------------------------------------------------------------------------------------
  320.  
  321. void CBitmapContent::InternalizePICTFile(Environment* ev, ODStorageUnit* storageUnit)
  322. {
  323.     HFSFlavor hfsInfo;
  324.     if (FW_MacSUReadHFSFlavor(ev, storageUnit, kODPropContents, hfsInfo))
  325.     {
  326.         // Use the FileSpec in HFS flavor to open the file's data fork
  327.         FW_PFileSpecification fileSpec(ev, hfsInfo.fileSpec);
  328.         FW_CAccessPermission permission(FW_kRead, FW_kDenyWrite);
  329.         FW_PFile pictFile(ev, fileSpec, permission);
  330.         FW_PFileSink pictSink(ev, pictFile);
  331.     
  332.         // Read the picture data from the file
  333.         #define kPICTHeaderSize 512
  334.         unsigned long pictSize = pictSink->GetLength(ev) - kPICTHeaderSize;
  335.         
  336.         FW_CAcquireTemporarySystemHandle tempHandle(pictSize);
  337.         
  338.         pictSink->SetPosition(ev, kPICTHeaderSize);    // skip PICT file header
  339.         pictSink->Read(ev, tempHandle.GetPointer(), pictSize);
  340.         
  341.         MacSetBitmap(ev, (FW_PlatformPict)tempHandle.GetPlatformHandle());
  342.     }
  343. }
  344. #endif
  345.  
  346. #ifdef FW_BUILD_MAC
  347. //----------------------------------------------------------------------------------------
  348. //    CBitmapContent::MacSetBitmap
  349. //----------------------------------------------------------------------------------------
  350. //    Set the bitmap from from a pict handle. The FW_PlatformPict should be deleted by the 
  351. //    caller
  352.  
  353. void CBitmapContent::MacSetBitmap(Environment* ev, FW_PlatformPict picHandle)
  354. {
  355. FW_UNUSED(ev);
  356.     FW_CPicture picture(picHandle);            // Does not take ownership    
  357.     fBitmap = FW_CBitmap(picture);
  358. }
  359. #endif
  360.  
  361. //========================================================================================
  362. //    class CBitmapSelectionContent
  363. //========================================================================================
  364.  
  365. //----------------------------------------------------------------------------------------
  366. //    CBitmapSelectionContent::CBitmapSelectionContent
  367. //----------------------------------------------------------------------------------------
  368. //    CBitmapSelectionContent constructor
  369.  
  370. CBitmapSelectionContent::CBitmapSelectionContent(Environment* ev, CBitmapPart* part, CBitmapSelection* selection, CBitmapContent* bitmapContent) :
  371.     FW_CContent(ev, part),
  372.     fBitmapPart(part),
  373.     fBitmapContent(bitmapContent),
  374.     fBitmapSelection(selection)    
  375. {
  376. }
  377.  
  378. //----------------------------------------------------------------------------------------
  379. //    CBitmapSelectionContent::~CBitmapSelectionContent
  380. //----------------------------------------------------------------------------------------
  381. //    CBitmapSelectionContent destructor
  382.  
  383. CBitmapSelectionContent::~CBitmapSelectionContent()
  384. {
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. //    CBitmapSelectionContent::SelectEntireBitmap
  389. //----------------------------------------------------------------------------------------
  390.  
  391. void CBitmapSelectionContent::SelectEntireBitmap(Environment* ev)
  392. {
  393.     short width, height,rowBytes, pixelSize;
  394.     fBitmapContent->GetBitmap(ev).GetBitmapInfo(width, height, rowBytes, pixelSize);
  395.     FW_CPlatformRect bounds(0, 0, width, height);
  396.     SetSelectRect(bounds);
  397. }
  398.  
  399. //----------------------------------------------------------------------------------------
  400. //    CBitmapSelectionContent::InternalizeKind
  401. //----------------------------------------------------------------------------------------
  402. //    ODF method
  403.  
  404. FW_Boolean CBitmapSelectionContent::InternalizeKind(Environment* ev,
  405.                                                 ODStorageUnit* storageUnit, 
  406.                                                 FW_CKind* kind,
  407.                                                  FW_StorageKinds storageKind,
  408.                                                 FW_CCloneInfo* cloneInfo)
  409. {
  410.     fBitmapSelection->CloseSelection(ev);
  411.  
  412.     FW_Boolean result = fBitmapContent->InternalizeKind(ev, storageUnit, kind, storageKind, cloneInfo);
  413.     
  414.     if (result)
  415.     {
  416.         fBitmapPart->AdjustFramesSize(ev);
  417.         fBitmapPart->UpdatePresentation(ev);    
  418.     }
  419.     
  420.     return result;
  421. }
  422.  
  423. //----------------------------------------------------------------------------------------
  424. //    CBitmapSelectionContent::ExternalizeKind
  425. //----------------------------------------------------------------------------------------
  426. //    ODF method
  427.  
  428. void CBitmapSelectionContent::ExternalizeKind(Environment* ev,
  429.                                            ODStorageUnit* destinationSU,
  430.                                         FW_CKind* kind,
  431.                                           FW_StorageKinds storageKind,
  432.                                           FW_CPromise* promise,
  433.                                          FW_CCloneInfo* cloneInfo)
  434. {
  435. FW_UNUSED(storageKind);
  436. FW_UNUSED(cloneInfo);
  437. FW_UNUSED(promise);
  438.     
  439.     FW_CBitmap bitmap = fBitmapContent->GetBitmap(ev).Copy(fSelectRect);
  440.  
  441.     fBitmapContent->DoExternalize(ev, destinationSU, kind, bitmap);
  442. }
  443.  
  444. //---------------------------------------------------------------------------------------
  445. //    CBitmapSelectionContent::AcquireSuggestedFrameShape
  446. //---------------------------------------------------------------------------------------
  447. // Return the shape of the selected data. This will be added to the clipboard or
  448. // drag and drop storage unit.
  449.  
  450. ODShape* CBitmapSelectionContent::AcquireSuggestedFrameShape(Environment* ev)
  451. {
  452.     ODShape* shape = NULL;
  453.     
  454.     // ----- we don't use the zoom factor -----
  455.     FW_CRect rect(fSelectRect);
  456.     if (rect != FW_kZeroRect)
  457.     {
  458.         rect.Offset(-rect.left, -rect.top);
  459.         shape = ::FW_NewODShape(ev, rect);
  460.     }
  461.     
  462.     return shape;
  463. }
  464.  
  465.